home *** CD-ROM | disk | FTP | other *** search
- Path: cscsun3.larc.nasa.gov!hook
- From: hook@cscsun3.larc.nasa.gov (Ed Hook)
- Newsgroups: comp.lang.c
- Subject: Re: Weird perl-C interaction (rand)
- Date: 17 Jan 1996 22:28:02 GMT
- Organization: CSC/NASA Langley Research Center
- Distribution: world
- Message-ID: <4djt5i$fjn@reznor.larc.nasa.gov>
- References: <4djfjg$l45@decaxp.harvard.edu>
- Reply-To: hook@cscsun3.larc.nasa.gov
- NNTP-Posting-Host: cscsun3.larc.nasa.gov
- Keywords: perl, C, rand, random
-
- In article <4djfjg$l45@decaxp.harvard.edu>, Zorro <berriz@husc.harvard.edu> writes:
- |>
- |>
- |> I tried the following in a perl script to generate seeds to be given
- |> as arguments in calls to a C program:
- |>
- |> $seed = int(rand((2**31) -1)) + 1;
- |>
- |> The C program stores this value in its own local variable "seed" and,
- |> of course, uses it to seed its random number generator:
- |>
- |> srandom(seed);
- |>
- |> Then, the C program calls random(), and takes the modulo 32768 of the
- |> returned value.
- |>
- |> u = random() % 32768;
- |>
- |> To my utter wilderment the resulting number u is *always* the same,
- |> even though the seeds produced by the perl script are always
- |> different.
- |>
- |> I got around the problem by switching the perl code to
- |>
- |> $seed = int(rand((2**14) -1)) + 1;
- |>
- |> ...but I'm clueless as to what's going on. Does anybody know? More
- |> to the point, is my "solution" OK?
- |>
- |> FWIW, I'm running this on an IBM RS/6000, under AIX 3.2.5. Below are
- |> toy versions of the perl script ("mumble") and the C program
- |> ("frotz.c", compiled to the executable "frotz"); they exhibit the
- |> problem described above. I've also included mumble's output.
- |>
- |> Thanks in advance,
- |>
- |> Z.
- |>
- |>
- |> ... mumble ..................................................................
- |>
- |> #!/usr/local/bin/perl -w
- |>
- |> srand(time|$$);
- |>
- |> for(1..10) {
- |> $seed = int(rand((2**31) -1)) +1;
- |> system("frotz $seed");
- |> }
- |>
- |> ..........................................................................
- |>
- |> /* frotz.c */
- |> void main(int argc, char *argv[]) {
-
- Oops -- you've misspelled "int main(int argc, char *argv[]) {"
- |>
- |> int x;
- |> unsigned int seed;
- |>
- |> seed = (unsigned) atoi(argv[1]);
- |> srandom(seed);
- |> x = random();
- |>
- |> printf("%12d %12d %6d\n", seed, x, x % 32768);
- |> }
- |>
- |> ..........................................................................
- |> $ mumble
- |> 494862336 1155565115 1595
- |> 614662144 884639291 1595
- |> 963575808 1387824699 1595
- |> 2010775552 1296303675 1595
- |> 1405616128 1142359611 1595
- |> 84934656 994772539 1595
- |> 1688469504 272434747 1595
- |> 1452736512 1738311227 1595
- |> 402128896 1452213819 1595
- |> 1499332608 789448251 1595
-
- It looks like you've uncovered a serious problem -- I modified 'frotz'
- so that it dumps out the first 10 values returned by 'random()', along
- with their reductions mod 32768. It appears that, regardless of the
- seed value, the reductions are always the *same* sequence; here are three
- examples:
-
- seed = 1184563200 :
- 765888059 1595
- 770629765 24709
- 1234495940 27076
- 959331147 15179
- 1640054444 16044
- 414280009 26953
- 1821691921 20497
- 1815332352 17920
- 1396069928 22056
- 715464796 8284
- seed = 434831360 :
- 1441695291 1595
- 1543430277 24709
- 191687108 27076
- 1713257291 15179
- 749813420 16044
- 1770613065 26953
- 1160040465 20497
- 10733056 17920
- 1175344680 22056
- 194846812 8284
- seed = 987693056 :
- 1582335547 1595
- 1931665541 24709
- 13822404 27076
- 62536523 15179
- 1036861100 16044
- 682584393 26953
- 132698129 20497
- 186631680 17920
- 1102337576 22056
- 1846222940 8284
-
- This doesn't seem to square very well with the claims put forth in the
- AIX (4.1.3) man pages:
-
- "The random and srandom subroutines have almost the same calling sequence
- and initialization properties as the rand and srand subroutines. The
- difference is that the rand subroutine produces a much less random
- sequence; in fact, the low dozen bits generated by the rand subroutine
- go through a cyclic pattern. All the bits generated by the random
- subroutine are usable. For example, random( )&01 produces a random
- binary value." (Quoted without permission ... obviously.)
-
- Although the behavior discovered above doesn't make the final claim untrue,
- I would certainly wonder about the "randomness" of a sequence that always
- looks exactly the same modulo a not-too-large power of 2 ...
-
- Any other comments ?
-
- --
- Ed Hook | Coppula eam, se non posit
- Computer Sciences Corporation | acceptera jocularum.
- NASA Langley Research Center | Me? Speak for my employer?...<*snort*>
- Internet: hook@cscsun3.larc.nasa.gov | ... Get a _clue_ !!! ...
-